home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / frbts_20.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1991-04-28  |  11KB  |  428 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    File.c      Version 2.0    By Craig Derouen                           */
  4. /*                File i/o processsing module for Frobot.c                  */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /****************************************************************************/
  8.  
  9. #include "frobot.h"
  10.  
  11. #include <share.h>
  12. #include <time.h>
  13. #include "globals.h"
  14.  
  15. /* Parse out the parameter file */
  16. int getparms(char *ctlfile)
  17. {
  18.    FILE *infile;
  19.    char *p1,*p2;
  20.    int checkcount = 0;
  21.  
  22.    if ((infile = _fsopen(ctlfile,"rt",SH_DENYNO)) == NULL)
  23.       return(TRUE);        /* Say there's an error */
  24.  
  25.    fgets(temp,81,infile);
  26.    while (!feof(infile)) {
  27.       if (*temp) {        /* Parse out token string */
  28.          p1 = strtok(temp," \n\t");
  29.          p2 = strtok(NULL," \n\t;");
  30.          if (*p1 == ';') {        /* Just ignore comment lines */
  31.             fgets(temp,81,infile);
  32.             continue;
  33.          }
  34.          if (strcmpi(p1,"node") == 0) {
  35.             if (p2) {     
  36.                if (!parsenet(p2,&Zone,&Net,&Node))
  37.                   checkcount++;
  38.             }
  39.          }
  40.          else if (strcmpi(p1,"outbound") == 0) {
  41.             if (p2) {
  42.                striptail(p2);
  43.                strcpy(Outbdir,p2);
  44.                checkcount++;
  45.             }
  46.          }
  47.          else if (strcmpi(p1,"inbound") == 0) {
  48.             if (p2){
  49.                striptail(p2);
  50.                strcpy(Inbdir,p2);
  51.                checkcount++;
  52.             }
  53.          }
  54.          else if (strcmpi(p1,"netmail") == 0) {
  55.             if (p2) {
  56.                striptail(p2);
  57.                strcpy(Netmdir,p2);
  58.                checkcount++;
  59.             }
  60.          }
  61.          else if (strcmpi(p1,"scriptloc") == 0) {
  62.             if (p2) {
  63.                strcpy(ScriptFile,p2);
  64.                checkcount++;
  65.             }
  66.          }
  67.          else if (strcmpi(p1,"logfile") == 0) {
  68.             if (p2) strcpy(LogFile,p2);
  69.          }
  70.          else if (strcmpi(p1,"display") == 0) {
  71.             if (p2){
  72.                if (strcmpi(p2,"Yes") == 0)
  73.                   ShowActivity = TRUE;
  74.             }
  75.          }
  76.          else if (strcmpi(p1,"selfmail") == 0) {
  77.             if (p2){
  78.                if (strcmpi(p2,"Yes") == 0)
  79.                   SelfMail = TRUE;
  80.             }
  81.          }
  82.          else if (strcmpi(p1,"sysopmail") == 0) {
  83.             if (p2){
  84.                if (strcmpi(p2,"Yes") == 0)
  85.                   SysopMail = TRUE;
  86.             }
  87.          }
  88.          else if (strcmpi(p1,"bells") == 0) {
  89.             if (p2){
  90.                if (strcmpi(p2,"Yes") == 0)
  91.                   Silent = FALSE;
  92.                else Silent = TRUE;
  93.             }
  94.          }
  95.       }
  96.       fgets(temp,81,infile);
  97.    }
  98.    fclose(infile);
  99.    if (checkcount < 5)
  100.       return(2);        /* Not all the necessary parameters were found */
  101.    return(FALSE);
  102. }
  103.  
  104. /* Process the script file */
  105. int doscript(void)
  106. {
  107.    FILE *sfile;
  108.    FILE *bfile;
  109.    char *linebuff,*p;
  110.    char backup[_MAX_PATH];
  111.    int code = FALSE;
  112.  
  113. /* First thing is to create a backup file for the script */
  114.    createbackup(ScriptFile,backup);
  115.  
  116.    if ((bfile = _fsopen(backup,"wt",SH_DENYRW)) == NULL) 
  117.       aborterror(BADFILE,NULL);
  118.  
  119.    if ((sfile = _fsopen(ScriptFile,"rt",SH_DENYNO)) == NULL)
  120.       aborterror(BADFILE,NULL);
  121.  
  122.    time(&now);
  123.    ltime = localtime(&now);
  124.    p = strtok(asctime(ltime)," ");
  125.    strcpy(datestr,p);
  126.    strcat(datestr,", ");
  127.    strcat(datestr,strtok(NULL," "));
  128.    strcat(datestr," ");
  129.    strcat(datestr,strtok(NULL," "));
  130.    strcat(datestr," ");
  131.    strtok(NULL," ");
  132.    strcat(datestr,strtok(NULL," \n"));
  133.  
  134.    linebuff = (char *) malloc(MAXLINE_SIZE+1);
  135.    if (linebuff == NULL)
  136.       aborterror(NOMEM,NULL);
  137.  
  138.    linenum = 0;
  139.    FidoMsgNote[0] = 0;
  140.    fgets(linebuff,MAXLINE_SIZE,sfile);
  141.    while (!feof(sfile)) {
  142.       linenum++;
  143.       if (*linebuff) {        /* Parse out token string */
  144.          if (*linebuff != ';' && *linebuff != '\n') 
  145.             procline(linebuff);
  146.       }
  147.       if (*linebuff != '\n')        /* Remove blank lines! */
  148.          fputs(linebuff,bfile);
  149.       fgets(linebuff,MAXLINE_SIZE,sfile);
  150.    }
  151.  
  152.    free(linebuff);
  153.    fclose(bfile);
  154.    fclose(sfile);
  155.  
  156.    if(remove(ScriptFile) == 0) 
  157.       rename(backup,ScriptFile);
  158.    else {
  159.       warble();
  160.       printf("Error changing script file\n");
  161.       code = -1;
  162.       remove(backup);
  163.    }
  164.    return(code);
  165. }
  166.  
  167. /* Process valid line from script file */
  168. void procline(char *line)
  169. {
  170.    char *workline;
  171.    char *p1,*p2,*fname;
  172.    int x;
  173.  
  174.    blanktrim(line);
  175.    strcat(line,"\n");
  176.    if (*line == '\n')
  177.       return;        /* It's a blank line */
  178.  
  179. /* Create a 2nd copy of the script line for munging with */
  180.    workline = (char *) malloc(strlen(line) + 10);
  181.    if (workline == NULL)
  182.       aborterror(NOMEM,NULL);
  183.    strcpy(workline,line);
  184.  
  185.  
  186.    if (ShowActivity)
  187.       printf("Processing line # %d\n",linenum);
  188.    p1 = strtok(workline," ;\t\n");        /* Initial token finds out send or rcv */
  189.    strlwr(p1);        /* Makes testing easier */
  190.    p2 = strtok(NULL,"\n");        /* get rest of line */
  191.    if (!validate(p2,line))    {    /* Check date/time */
  192.       p2 = p1;
  193.       p2 += strlen(p1) + 1;
  194.       blanktrim(p2);
  195.       fname = strtok(p2," ;\t\n");        /* Bump to node */
  196.       for (x=0; x < 4; x++)
  197.          p2 = strtok(NULL," ;\t\n");        /* Bump rest */
  198. /* p2 now has fidonet mode */
  199.       FidoNetMode = (char) toupper(*p2);
  200. /* Build flowfile and req filenames */
  201.       strcpy(FloFile,Outbdir);
  202.       strcpy(ReqFile,Outbdir);
  203.       sprintf(temp,"\\%04x%04x.",DestNet,DestNode);
  204.       strcat(ReqFile,temp);
  205.       strcat(ReqFile,"REQ");
  206.       if (FidoNetMode == 'N')
  207.          strcat(temp,"FLO");
  208.       else if (FidoNetMode == 'N')
  209.          strcat(temp,"DLO");
  210.       else {
  211.          x = strlen(temp);
  212.          temp[x] = FidoNetMode;
  213.          temp[x+1] = 0;
  214.          strcat(temp,"LO");
  215.       }
  216.       strcat(FloFile,temp);
  217.       p2 = strtok(NULL," =;\n");        /* Bump to rest */
  218.       if (p2) blanktrim(p2);        /* Bump to count */
  219.  
  220.       if (*p1 == 's')     /* File send */
  221.          file_send(p1,line,fname);
  222.       else if (*p1 == 'r')    {    /* file request */
  223.          if (p2) {
  224.             FileCount = atoi(p2);
  225.             p2 = strtok(NULL," ;\n");        /* See if there's a xfercount */
  226.             if (p2) XferTries = atoi(p2);
  227.             else XferTries = -1;
  228.             file_request(line,fname);
  229.          }
  230.          else {        /* No count, try it once! */
  231.             FileCount = 1;
  232.             XferTries = -1;
  233.    /* Fix up line */
  234.             x = strlen(line);
  235.             line[x-1]=0;        /* Strip linefeed */
  236.             strcat(line," 1\n");
  237.             file_request(line,fname);
  238.          }
  239.       }
  240.       /* Anything else we just ignore and comment out! */
  241.       else commentline(line," Invalid command,ignored!");
  242.    }
  243.    free(workline);
  244. }
  245.  
  246.  
  247.  
  248. /* Check dirpath and remove tail dir sep if its there */
  249. void striptail(char *p)
  250. {
  251.    int x;
  252.  
  253.    x = strlen(p) - 1;
  254.    if (p[x] == '\\')
  255.       p[x] = 0;
  256. }
  257.  
  258. /* Comment out the current line and append possible string */
  259. void commentline(char *line,char *str)
  260. {
  261.    char *p1;
  262.  
  263.    strinsert(";",line,0);        /* Force it to be ignored next time */
  264.    if (str != NULL) {
  265.       p1 = strchr(line,'\n');
  266.       if(p1 != NULL)
  267.          *p1 = 0;        /* Remove linefeed so we can append */
  268.       strcat(line," <-");
  269.       strcat(line,str);
  270.       if (!strchr(str,'\n'))
  271.          strcat(line,"\n");
  272.    }
  273. }
  274.  
  275. /* Test for valid date/time
  276.    return -1 if error, 0 if okay, 1 if don't process */
  277. int validate(char *input,char *line)
  278. {
  279.    char *datep;
  280.    char *timep;
  281.    char *savep;
  282.    char *p1;
  283.    int x,code;
  284.  
  285. /* input -> token line, line -> original line */
  286.  
  287.    p1 = (char *) malloc(strlen(input) + 1);
  288.    if (p1 == NULL)
  289.       aborterror(NOMEM,NULL);
  290.    strcpy(p1,input);
  291.  
  292.    if(setdest(p1,line)) {        /* setup the global node destination,file */
  293.       free(p1);
  294.       return(-1); /* An error occured */
  295.    }
  296.    savep = strtok(p1," ");        /* Bump past filename */
  297.    savep = strtok(NULL," ");        /* Bump past node */
  298.    savep = strtok(NULL,"\n");        /* Bump past node */
  299.    datep = strtok(savep," ");        /* Get date string */
  300.    timep = strtok(NULL," ");        /* Get time string */
  301.    if (datep) {
  302.       x = datecheck(datep);
  303.       switch (x) {
  304.          case -2:     /* Invalid date string */
  305.             commentline(line,"Invalid date!");
  306.             code = -1;
  307.             break;
  308.  
  309.          case -1:        /* Older so process now! */
  310.             code = 0;
  311.             break;
  312.  
  313.          case 0:        /* Today, so check time first */
  314.             x = timecheck(timep);
  315.             if (x == -2) {
  316.                commentline(line,"Invalid time!");
  317.                code = -1;
  318.             }
  319.             else if (x == -1 || x == 0)     /* Ok to process now */
  320.                code = 0;
  321.             else code = 1;        /* Not current yet */
  322.             break;
  323.  
  324.          default:        /* Well don't touch anything */
  325.             code = 1;
  326.             break;
  327.       }
  328.    }
  329.    else {
  330.          commentline(line,"Invalid date!");
  331.          code = -1;
  332.    }
  333.    free(p1);
  334.    return(code);
  335. }
  336.  
  337. /* Check to see if filename is inside infile (text mode)
  338.    return TRUE if so */
  339. int filescan(FILE *infile,char *fname)
  340. {
  341.    static char fileline[100];
  342.    char *p;
  343.  
  344.    rewind(infile);
  345.    while(fgets(fileline,99,infile)) {
  346.       p = strtok(fileline," \n");        /* Strip out file */
  347.       if (*p == '#' || *p == '^')        /* Special kludges for flo files */
  348.          p++;
  349.       if(strcmpi(p,fname) == 0) 
  350.          return(TRUE);
  351.    }
  352.    return(FALSE);
  353. }
  354.  
  355.  
  356. /* Create a backup file with ext ".$$$" */
  357. void createbackup(char *input,char *output)
  358. {
  359.    int x;
  360.    char *p;
  361.  
  362.    strcpy(output,input);
  363.    x = strlen(output) - 1;
  364.    p = &output[x];
  365.    while (*p != '.' && *p != '\\')
  366.       p--;
  367.  
  368.    switch (*p) {
  369.  
  370.       case '\\':        /* Humm, a fullpath/filename with no extension */
  371.          p++;
  372.          strcat(p,".$$$");
  373.          break;
  374.  
  375.       case '.':
  376.          p++;
  377.          strcpy(p,"$$$");
  378.          break;
  379.  
  380.    }
  381. }
  382.  
  383. void logit(char *strng,char flag)
  384. {
  385.    FILE *log;
  386.    char ttemp[40];
  387.    char *ttemp1;
  388.    char *month;
  389.    char *day;
  390.    char *tm;
  391.  
  392.    if (LogFile[0] == 0)
  393.       return;
  394.  
  395.    ttemp1 = (char *) malloc(101);
  396.    if (ttemp1 == NULL) {
  397.       LogFile[0] = 0;
  398.       aborterror(NOMEM,NULL);
  399.    }
  400.  
  401.    strcpy(ttemp, timestring());
  402.    strtok(ttemp," ");               /* Strip Day of week */
  403.    month = strtok(NULL," ");
  404.    day = strtok(NULL," ");
  405.    tm = strtok(NULL," ");
  406.  
  407.    sprintf(ttemp1,"%c %s %s %s Frobot  %s\n",flag,day,month,tm,strng);
  408.    if ((log = _fsopen(LogFile,"a",SH_DENYWR)) == NULL) {
  409.       printf("\a\aError opening log file!\n");
  410.       LogFile[0] = 0;
  411.       free(ttemp1);
  412.       return;
  413.    }
  414.    fputs(ttemp1,log);
  415.    free(ttemp1);
  416.    fclose(log);
  417. }
  418.  
  419. char *timestring(void)
  420. {
  421.    time_t     aclock;
  422.    struct tm  *newtime;
  423.  
  424.    time(&aclock);
  425.    newtime = localtime(&aclock);
  426.    return(asctime(newtime));
  427. }
  428.